package com.feihong.txgw.tx_model;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.feihong.txgw.inter.contract.order.IPlaceOrderContract;
import com.feihong.txgw.model.bean.deliveryAdr.DeliveryAdrEntity;
import com.feihong.txgw.ui.activity.user.MyDeliveryAdrActivity;
import com.feihong.txgw.utils.PreferenceTools;
import com.feihong.txgw.utils.adress.StringEmpty;
import com.feihong.txgw.utils.helper.RxHelper;
import com.feihong.txgw.utils.kaika.KaikaIntentType;
import com.feihong.txgw.utils.manage.RetrofitManager;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName PlaceOrderMainModel
 * @package com.feihong.txgw.tx_model
 * @data 2018/5/7 15:42
 * @Describe
 * @Change
 */
public class PlaceOrderMainModel implements IPlaceOrderContract.IPlaceOrderMainModel {
    private DeliveryAdrEntity mDeliveryAdrEntity;
    @Override
    public Observable<String> submitTerminalOrder(String adrID,String type,String renark,String goodsInfo) {
        Map<String,String> mParams = new HashMap<>();
        mParams.put("addr_id",adrID);
        mParams.put("remark",renark);
        mParams.put("type",type);
        mParams.put("goods_info",goodsInfo);
        return RetrofitManager.getInstance().submitTerminalOrder(mParams).compose(RxHelper.<String>rxSchedulerHelper());
    }

    @Override
    public Observable<DeliveryAdrEntity> parseBundle(Intent data) {
        if (data != null) {
            Bundle bundle = data.getExtras();
            if (bundle == null) {
                return Observable.just(mDeliveryAdrEntity);
            }
            mDeliveryAdrEntity = bundle.getParcelable(KaikaIntentType.TERMINAL_ORDER_SELECT_ADR_KEY);
        } else {
            // 如果选择了地址则显示选择的地址信息 如果没有选择地址 则展示默认的收货地址
            if (TextUtils.isEmpty(PreferenceTools.getString(MyDeliveryAdrActivity.SELECT_ADR_KEY, ""))) {
                if (TextUtils.isEmpty(PreferenceTools.getString(MyDeliveryAdrActivity.DEFAULT_ADR_KEY, ""))) {
                    mDeliveryAdrEntity = new DeliveryAdrEntity();
                } else {
                    mDeliveryAdrEntity = JSON.parseObject(PreferenceTools.getString(MyDeliveryAdrActivity.DEFAULT_ADR_KEY, null), DeliveryAdrEntity.class);
                    // 兼容1.1.0版本 如果是老地址的话 会因为新接口的变动缺少返回参数   等同于没有地址信息
                    if (mDeliveryAdrEntity == null || StringEmpty.isEmpty(mDeliveryAdrEntity.getAddress()) || StringEmpty.isEmpty(mDeliveryAdrEntity.getLocation_info())) {
                        mDeliveryAdrEntity = null;
                    }
                }
            } else {
                mDeliveryAdrEntity = JSON.parseObject(PreferenceTools.getString(MyDeliveryAdrActivity.SELECT_ADR_KEY, null), DeliveryAdrEntity.class);
                // 兼容1.1.0版本 如果是老地址的话 会因为新接口的变动缺少返回参数   等同于没有地址信息  可删除
                if (mDeliveryAdrEntity == null || StringEmpty.isEmpty(mDeliveryAdrEntity.getAddress())|| StringEmpty.isEmpty(mDeliveryAdrEntity.getLocation_info())) {
                    mDeliveryAdrEntity = new DeliveryAdrEntity();
                }
            }
        }

        return Observable.just(mDeliveryAdrEntity);
    }

    public static PlaceOrderMainModel create(){
        return new PlaceOrderMainModel();
    }
}
